import React from 'react';
import {Part} from "type/Part";
import {Form, Select} from "antd";
import {DefaultOptionType} from "rc-select/lib/Select";

import useAsyncEffect from "../../../hooks/useAsyncEffect";
import DrpSettingHandler from "../DrpSettingHandler";
import {getPartGroupOptions} from "../SeleteOptionHanlder";
import {PartFormSelectProps} from "../init";

/**
 * 零件分组选择器
 */
const PartGroupSelect = ({ form,defaultValue,disableValue }: PartFormSelectProps) => {
    const [loading,setLoading] = React.useState<boolean>(true) ;
    const [options,setOptions] = React.useState<DefaultOptionType[]>([]) ;

    useAsyncEffect( async ()=>{
        setOptions(await getPartGroupOptions()) ;
       if (!disableValue){
           defaultValue = defaultValue || DrpSettingHandler.getInstance().getConfig().defaultPartGroup ;
           form.setFieldValue("partGroup",defaultValue) ;
       }
        setLoading(false) ;
    } ) ;

    return (
        <Form.Item<Part> name={"partGroup"} label="零件分组" required>
            <Select loading={loading} options={options}/>
        </Form.Item>
    );
};

export default PartGroupSelect;